package com.ocom.wxapi.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.GetFlowkeyByPidRequest;
import com.ocom.common.request.company.NewApplyRequest;
import com.ocom.common.request.human.NewLeaveApplyRequest;
import com.ocom.common.request.wx.DBStudentRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.FlowConfigVo;
import com.ocom.common.vo.human.UserInfoVo;
import com.ocom.common.vo.ugcapi.GzhUserInfo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.wxapi.client.ActivitiClientService;
import com.ocom.wxapi.client.CompanyClientService;
import com.ocom.wxapi.client.HumanClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * 侨初公众号代请假
 */

@Slf4j
@RestController
@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/gzhQC")
public class GzhQcController {

    @Autowired
    ActivitiClientService activitiClientService;

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    CompanyClientService companyClientService;

    private final String leaveKey  = "leave-apply";


    //学生列表
    @PostMapping("/getStudentList")
    public Result list(@Valid UserInfoVo userInfoVo) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        userInfoVo.setComId(yoCiUser.getComId());
        userInfoVo.setDeptId(Long.valueOf(yoCiUser.getDeptId()));
        userInfoVo.setPage(1);
        userInfoVo.setSize(500);
        return humanClientService.PersonnelGetPersonInfo(userInfoVo);
    }


    @PostMapping("/gzhLeaveApply")
    public Result gzhLeaveApply(@RequestBody DBStudentRequest request){
         if(request.getPids().size()<=0){
            return ResultUtil.error(ResultEnum.Err_4017);
         }

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

        GetFlowkeyByPidRequest getFlowkeyByPidRequest = new GetFlowkeyByPidRequest();
        getFlowkeyByPidRequest.setFlowCode("leave-apply");
        getFlowkeyByPidRequest.setComId(yoCiUser.getComId());
        getFlowkeyByPidRequest.setPId(request.getPids().get(0).intValue());
        Result<FlowConfigVo> result3 = this.companyClientService.getFlowkeyByPid(getFlowkeyByPidRequest);
        if (result3.getCode() != 200) {
            return result3;
        }

        for (Long pId : request.getPids()){

            GzhUserInfo userInfo = new GzhUserInfo();
            userInfo.setComId(yoCiUser.getComId());
            userInfo.setPId(pId);   //  dept 1622
            int type = request.getType();
            String timeS = request.getStartTime();
            String timeE = request.getEndTime();
            String reason = request.getReason();
            String pic = request.getPic();
            Date startTime = DateUtil.parse(timeS, "yyyy/MM/dd HH:mm:ss");
            Date endTime = DateUtil.parse(timeE,"yyyy/MM/dd HH:mm:ss");

            FlowConfigVo flowConfigVo = result3.getData();
            NewLeaveApplyRequest newLeaveApplyRequest = Convert.convert(NewLeaveApplyRequest.class,request);
            newLeaveApplyRequest.setComId(userInfo.getComId());
            newLeaveApplyRequest.setPId(userInfo.getPId());
            newLeaveApplyRequest.setStartTime(startTime);
            newLeaveApplyRequest.setEndTime(endTime);
            newLeaveApplyRequest.setPic(pic);
            newLeaveApplyRequest.setReason(reason);
            newLeaveApplyRequest.setApplyDeptId(flowConfigVo.getDeptId().longValue());
            newLeaveApplyRequest.setDays(new BigDecimal(com.ocom.common.utils.DateUtil.countWorkingDays(startTime,endTime)));
            Result<EntityBizLeaveApply> resultA = this.companyClientService.newLeaveApply(newLeaveApplyRequest);

            NewApplyRequest newApplyRequest = new NewApplyRequest();
            newApplyRequest.setComId(userInfo.getComId());
            newApplyRequest.setType(type);
            newApplyRequest.setPId(userInfo.getPId());
            newApplyRequest.setBusinessId(resultA.getData().getId());
            newApplyRequest.setApplyDeptId(flowConfigVo.getDeptId());
            newApplyRequest.setDefKey(result3.getData().getDefKey());
            newApplyRequest.setTitle(flowConfigVo.getDeptName()+flowConfigVo.getPName()+"申请"+type);

            HashMap<String,Object> param = new HashMap<>();
            param.put("status",0);
            param.put("daiban",1);

            newApplyRequest.setParams(param);
            newApplyRequest.setVariables(result3.getData().getVariables());
            this.companyClientService.newApply(newApplyRequest);
        }

        return ResultUtil.success();
    }


}
